package models;

import play.api.Play;

import java.io.*;
import java.util.Collections;
import java.util.LinkedList;

import static play.mvc.Controller.session;

/**
 * Created with IntelliJ IDEA.
 * User: chlemke
 * Date: 19.01.14
 * Time: 23:33
 * To change this template use File | Settings | File Templates.
 */
public class HighscoreHelper {

    public class HighscoreLine implements Comparable<HighscoreLine>{
        public String[] sArray;

        public HighscoreLine(String s) {
            //read line
            // firstname;lastname;email;userId;imgurl;mapsize;monster;wave;
            sArray = new String[8];
            int part= 0;
            int lastPartEnd = 0;
            for(int i = 0; i< s.length() ; i++){
                if(s.charAt(i) == ';'){
                    sArray[part] = s.substring(lastPartEnd, i);
                    part++;
                    lastPartEnd = i+1;
                }

            }
        }

        public String getFirstName(){
            return sArray[0];
        }
        public String getLastName(){
            return sArray[1];
        }
        public String getEmail(){
            return sArray[2];
        }
        public String getUserId(){
            return sArray[3];
        }
        public String getImageUrl(){
            return sArray[4];
        }
        public String getMapSize(){
            return sArray[5];
        }
        public String getMonster(){
            return sArray[6];
        }
        public String getWave(){
            return sArray[7];
        }

        // generate string
        // firstname;lastname;email;userId;imgurl;mapsize;monster;wave;
        public String getFileStringLine(){
            String s = "";
            for(int i = 0; i < sArray.length; i++){
                s+= sArray[i] + ";";
            }

            return s;
        }

        @Override
        public int compareTo(HighscoreLine o) {
            if (Integer.parseInt(getMonster()) > Integer.parseInt(o.getMonster()))
                return -1;
            else if (Integer.parseInt(getMonster()) == Integer.parseInt(o.getMonster()))
                if (Integer.parseInt(getWave()) > Integer.parseInt(o.getWave())){
                    return -1;
                } else {
                    return 0;
                }
            else
                return 1;
        }
    }

    public LinkedList<HighscoreLine> getHighscores(){
        java.io.File myFile = Play.getFile("/app/highscore.txt", Play.current());
        try {
            FileReader fr = new FileReader(myFile);
            BufferedReader br = new BufferedReader(fr);
            try {
                String line = br.readLine();
                LinkedList<HighscoreLine> list = new LinkedList<HighscoreLine>();

                while (line != null) {
                    list.add(new HighscoreLine(line));
                    line = br.readLine();
                }

                return list;

            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }


        return null;
    }

    public String saveNewHighscore(){
        // get picture url
        String profileImgUrl = "https://plus.google.com/s2/photos/profile/"+ session("userid") +"?sz=60";
        String fileLine =  session("firstname")+";" + session("lastname")+";" + session("email")+";" + session("userId")+";" + profileImgUrl +";" + session("mapsize")+";" + session("monster")+";" + session("wave")+";";

        HighscoreLine hl = new HighscoreLine(fileLine);
        addToHighscoreList(hl);

        return session("userId");
    }

    private void addToHighscoreList(HighscoreLine hsl){
        LinkedList<HighscoreLine> highscores = getHighscores();
        highscores.add(hsl);
        Collections.sort(highscores);

        //save to file
        java.io.File myFile = Play.getFile("/app/highscore.txt", Play.current());
        try {
            FileWriter fr = new FileWriter(myFile);
            BufferedWriter br = new BufferedWriter(fr);
            try {
                for(HighscoreLine l: highscores){

                    br.append(l.getFileStringLine() + "\n");
                }

            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }



}
